using UnityEngine;
using System.Collections;

public class ZooMusicManager : MonoBehaviour
{
    public static ZooMusicManager Instance;

    [Header("Music Clips")]
    public AudioClip[] songs;       // Assign 3 background tracks
    public float fadeDuration = 2f;
    public float musicVolume = 0.75f;

    [Header("SFX Clips")]
    public AudioClip[] sfx;         // Assign your sound effects here
    public float sfxVolume = 1f;

    private AudioSource sourceA;
    private AudioSource sourceB;

    // Two SFX channels
    private AudioSource sfxA;
    private AudioSource sfxB;

    private int currentTrackIndex = 0;
    private bool usingA = true;
    private bool isFading = false;


    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
            SetupAudioSources();
        }
        else
        {
            Destroy(gameObject);
        }
    }


    // ---------------------------------------------------------
    // Setup music sources + SFX sources
    // ---------------------------------------------------------
    void SetupAudioSources()
    {
        // MUSIC
        sourceA = gameObject.AddComponent<AudioSource>();
        sourceA.loop = true;
        sourceA.volume = musicVolume;

        sourceB = gameObject.AddComponent<AudioSource>();
        sourceB.loop = true;
        sourceB.volume = 0f;

        // SFX A
        sfxA = gameObject.AddComponent<AudioSource>();
        sfxA.loop = false;
        sfxA.volume = sfxVolume;

        // SFX B
        sfxB = gameObject.AddComponent<AudioSource>();
        sfxB.loop = false;
        sfxB.volume = sfxVolume;
    }


    void Start()
    {
        if (songs.Length > 0)
            PlaySong(songs[0]);
    }

    // ---------------------------------------------------------
    // MUSIC PUBLIC API
    // ---------------------------------------------------------
    public void NextSong()
    {
        if (songs.Length == 0) return;

        currentTrackIndex = (currentTrackIndex + 1) % songs.Length;
        AudioClip nextClip = songs[currentTrackIndex];

        StartCoroutine(CrossfadeTo(nextClip));
    }


    // ---------------------------------------------------------
    // MUSIC INTERNAL HELPERS
    // ---------------------------------------------------------
    void PlaySong(AudioClip clip)
    {
        sourceA.clip = clip;
        sourceA.volume = musicVolume;
        sourceA.Play();
    }


    IEnumerator CrossfadeTo(AudioClip newClip)
    {
        if (isFading) yield break;
        isFading = true;

        AudioSource fadeOut = usingA ? sourceA : sourceB;
        AudioSource fadeIn = usingA ? sourceB : sourceA;

        fadeIn.clip = newClip;
        fadeIn.volume = 0f;
        fadeIn.Play();

        float t = 0f;

        while (t < fadeDuration)
        {
            t += Time.deltaTime;
            float lerp = t / fadeDuration;

            fadeOut.volume = Mathf.Lerp(musicVolume, 0f, lerp);
            fadeIn.volume = Mathf.Lerp(0f, musicVolume, lerp);

            yield return null;
        }

        fadeOut.Stop();
        usingA = !usingA;
        isFading = false;
    }

    // ---------------------------------------------------------
    // SFX PUBLIC API
    // ---------------------------------------------------------
    public void PlaySFX(int index)
    {
        if (sfx == null || sfx.Length == 0) return;
        if (index < 0 || index >= sfx.Length) return;

        AudioClip clip = sfx[index];
        if (clip == null) return;

        // Prefer sfxA; if busy, use sfxB
        if (!sfxA.isPlaying)
        {
            sfxA.pitch = 1f; // reset pitch if needed
            sfxA.clip = clip;
            sfxA.Play();
        }
        else
        {
            sfxB.pitch = 1f;
            sfxB.clip = clip;
            sfxB.Play();
        }
    }
}
